package com.corejsf;

import java.io.Serializable;
import java.util.List;

import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
@Named
@SessionScoped
public class MangeBean implements Serializable{
	
	@Inject
	Service service;
	private Categori categori, selectedCat;
	private Topic topic, selectedTop;
	
	
	public Categori getCategori(){
		return categori;
	}
	
	public boolean isSelectedCat(){
		return selectedCat != null;
	}
	
	public String newCategori(){
		selectedCat = null;
		categori = new Categori();
		return "categoriDetail";
	}
	
	public String selectCategori(Categori selected){
		this.selectedCat = selected;
		categori = selected.copy();
		return "topicList";
	}
	
	public String createCategori(){
		service.addCategori(categori);
		return toCategoriList();
	}
	
	public String updateCategori(){
		selectedCat.update(categori);
		return toCategoriList();
	}
	
	public String deleteCategori(){
		service.removeCategori(selectedCat);
		return toCategoriList();
	}
	
	public List<Categori> getCategoris(){
		return service.getCats();
	}
	
	public String toCategoriList(){
		selectedCat = null;
		return "categoriList";
	}
	
	//Topic bean
	public String createTopic(){
		service.addTopic(topic, categori);
		return toTopicList();
	}
	
	public void setCurrentCat(Categori categori){
		this.categori = categori;
	}
	
	public Topic getTopic(){
		return topic;
	}
	
	public boolean isSelectedTop(){
		return selectedTop != null;
	}
	
	public String newTopic(){
		selectedTop = null;
		topic = new Topic();
		return "newTopic";
	}
	
	public String selectTopic(Topic selected){
		this.selectedTop = selected;
		topic = selected.copy();
		return "topic";
	}
	
	public String updateTopic(){
		selectedTop.update(topic);
		return toTopicList();
	}
	
	public String deleteTopic(){
		service.removeTopic(selectedTop);
		return toTopicList();
	}
	
	public List<Topic> getTopics(){
		return service.getTopics(categori);
	}
	
	public String toTopicList(){
		selectedTop = null;
		return "topicList";
	}
	
	public void setCurrentTopic(Topic topic){
		this.topic = topic;
	}
	
	public String newComment(){
		return "newComment";
	}
	
	public String addComment(){
		service.CreateComment(selectedTop);
		topic.setComment("");
		return "topic";
	}
	
	public List<String> getComments(){
		return service.getComments(selectedTop);
	}
}
